package com.yanqu.road.server.cmd.wingroom.watchplayer;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

@Cmd(code = Protocol.S_WING_ROOM_CROSS_PULL_PATRONS_REQ, desc = "跨服来拉去top门客")
public class WingRoomCrossPullPlayerPatronsCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        WingRoomProto.CrossWatchOtherPlayerReqMsg reqMsg = WingRoomProto.CrossWatchOtherPlayerReqMsg.parseFrom(packet.getBytes());

        long playerId = reqMsg.getWatchPlayerId();

        Map<Integer, UserPatrons> topMap = WingRoomMgr.getTopUserPatrons(playerId);

        WingRoomProto.CrossPullPlayerPatronsRespMsg.Builder respMsg = WingRoomProto.CrossPullPlayerPatronsRespMsg.newBuilder();
        respMsg.setOriginPlayerUserId(reqMsg.getOriginPlayerId());
        respMsg.setOriginPlayerServerId(reqMsg.getOriginServerId());
        WingRoomProto.PullPlayerPatronsRespMsg.Builder pullMsg = WingRoomProto.PullPlayerPatronsRespMsg.newBuilder();
        pullMsg.setUserId(playerId);
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(playerId, GameServer.getInstance().getServerId());
        if(userBaseInfo != null){
            pullMsg.setBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }

        for (UserPatrons patrons : topMap.values()) {
            WingRoomProto.VisitPatronsTempMsg.Builder patronsTemp = WingRoomPb.buildVisitPatronsTemp(patrons);
            pullMsg.addPatrons(patronsTemp);
        }

        respMsg.setRespMsg(pullMsg);
        GamePlayerMgr.sendPacket(reqMsg.getOriginPlayerId(), YanQuMessageUtils.buildMessage(Protocol.C_WING_ROOM_CROSS_PULL_PLAYER_PATRONS_PLAYER_RESP, respMsg));

    }


}
